<?php
session_start();
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/includes/functions.php';

$message = '';

// 处理登录请求
if (isset($_POST['action']) && $_POST['action'] == 'login') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];
    
    $result = loginUser($username, $password);
    if ($result['success']) {
        header('Location: index.php');
        exit;
    } else {
        $message = $result['message'];
    }
}

// 处理注册请求
if (isset($_POST['action']) && $_POST['action'] == 'register') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $invite_code = trim($_POST['invite_code'] ?? '');
    
    if ($password !== $confirm_password) {
        $message = '两次输入的密码不一致';
    } else {
        $result = registerUser($username, $password, $invite_code);
        $message = $result['message'];
    }
}

// 获取邀请码参数
$invite_code_param = $_GET['invite'] ?? '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>登录/注册 - 投注游戏</title>
    <link rel="icon" href="static/img/logo.png" sizes="32x32" type="image/png">
    <link rel="stylesheet" href="assets/css/mobile.css">
    <style>
        /* 登录页面特殊样式 */
        .login-container {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }
        
        .login-box {
            background: white;
            border-radius: 25px;
            padding: 40px 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            width: 100%;
            max-width: 400px;
        }
        
        .login-box h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 28px;
        }
        
        .tabs {
            display: flex;
            margin-bottom: 30px;
            background: #f8f9fa;
            border-radius: 15px;
            padding: 5px;
        }
        
        .tab-btn {
            flex: 1;
            padding: 12px;
            border: none;
            background: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: bold;
            color: #666;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .tab-btn.active {
            background: #4caf50;
            color: white;
        }
        
        .auth-form {
            display: none;
        }
        
        .auth-form.active {
            display: block;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #333;
        }
        
        .form-group input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e0e0e0;
            border-radius: 15px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #4caf50;
        }
        
        button[type="submit"] {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #4caf50 0%, #45a049 100%);
            color: white;
            border: none;
            border-radius: 15px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: transform 0.2s;
        }
        
        button[type="submit"]:active {
            transform: scale(0.98);
        }
        
        .note {
            text-align: center;
            margin-top: 15px;
            color: #666;
            font-size: 14px;
        }
        
        .message {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 15px;
            background: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
            text-align: center;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-box">
            <h1>投注游戏</h1>
            
            <?php if ($message): ?>
                <div class="message"><?php echo htmlspecialchars($message); ?></div>
            <?php endif; ?>
            
            <div class="tabs">
                <button class="tab-btn active" onclick="showTab('login')">登录</button>
                <button class="tab-btn" onclick="showTab('register')">注册</button>
            </div>
            
            <!-- 登录表单 -->
            <form id="login-form" class="auth-form active" method="POST">
                <input type="hidden" name="action" value="login">
                <div class="form-group">
                    <label for="login-username">用户名:</label>
                    <input type="text" id="login-username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="login-password">密码:</label>
                    <input type="password" id="login-password" name="password" required>
                </div>
                <button type="submit">登录</button>
            </form>
            
            <!-- 注册表单 -->
            <form id="register-form" class="auth-form" method="POST">
                <input type="hidden" name="action" value="register">
                <div class="form-group">
                    <label for="register-username">用户名:</label>
                    <input type="text" id="register-username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="register-password">密码:</label>
                    <input type="password" id="register-password" name="password" required>
                </div>
                <div class="form-group">
                    <label for="confirm-password">确认密码:</label>
                    <input type="password" id="confirm-password" name="confirm_password" required>
                </div>
                <div class="form-group">
                    <label for="invite-code">邀请码 (可选):</label>
                    <input type="text" id="invite-code" name="invite_code" value="<?php echo htmlspecialchars($invite_code_param); ?>" placeholder="输入邀请码获得额外奖励">
                </div>
                <button type="submit">注册</button>
                <p class="note">注册即送1000元游戏币</p>
            </form>
        </div>
    </div>
    
    <script>
        function showTab(tab) {
            // 切换标签按钮状态
            document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
            event.target.classList.add('active');
            
            // 切换表单显示
            document.querySelectorAll('.auth-form').forEach(form => form.classList.remove('active'));
            document.getElementById(tab + '-form').classList.add('active');
        }
        
        // 页面加载完成后检查是否有邀请码参数
        document.addEventListener('DOMContentLoaded', function() {
            const inviteCode = '<?php echo htmlspecialchars($invite_code_param); ?>';
            if (inviteCode) {
                // 如果有邀请码，默认显示注册模块
                // 移除登录按钮的active类
                document.querySelector('.tab-btn[onclick="showTab(\'login\')"]').classList.remove('active');
                // 添加注册按钮的active类
                document.querySelector('.tab-btn[onclick="showTab(\'register\')"]').classList.add('active');
                
                // 隐藏登录表单
                document.getElementById('login-form').classList.remove('active');
                // 显示注册表单
                document.getElementById('register-form').classList.add('active');
                
                console.log('已自动切换到注册模块，邀请码:', inviteCode);
            }
        });
    </script>
</body>
</html>
